/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelHorasExtraEmpleado.java
 *
 * Created on 13/04/2011, 09:56:10 PM
 */

package frontera.interfaz.panel;

import control.SIRHApp;
import entidad.Empleado;
import entidad.HoraExtra;
import frontera.interfaz.dialogo.DialogCrearHoraExtra;
import frontera.interfaz.dialogo.DialogModificarHoraExtra;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author Silex RPR
 */
public class PanelHorasExtraEmpleado extends javax.swing.JPanel {

    /** Creates new form PanelHorasExtraEmpleado */
    public PanelHorasExtraEmpleado(Empleado empleado, List<HoraExtra> HE, JPanel panel) {
        initComponents();

        iniciarVariables(empleado, HE, panel);
        iniciarCalendario();

    }

    private void iniciarVariables(Empleado empleado, List<HoraExtra> HE, JPanel panel){
        this.empleado = empleado;
        propioPanel = this;
        labelNombreEmpleado.setText(empleado.getNombre());
        nuevasHorasExtras = HE;
        this.panel = panel;

    }

    private void iniciarCalendario(){
        ActionListener listener = new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                actualizarCalendario((JButton)e.getSource());
            }
        };

        PanelCalendario calendario = new PanelCalendario(listener);
        panelCalendario.add(calendario);

        for(HoraExtra he: nuevasHorasExtras){
            int dia = he.getFecha().get(Calendar.DATE);
            calendario.getBotones().get(dia-1).setBackground(Color.GREEN);
        }
    }

    private void actualizarCalendario(JButton boton){
        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.DATE, Integer.parseInt(boton.getText()));
        HoraExtra horaExtra = new HoraExtra( empleado.getCedula(), 0, cal, HoraExtra.tipoHora.Diurna);
        
        int indice = SIRHApp.getInstance().getSistema().getEmpleados().indexOf(empleado);

        if ( cantidadHorasExtras(nuevasHorasExtras) >= 24 ){
            JOptionPane.showMessageDialog(SIRHApp.getInstance().getVentanaPrincipal(), "La cantidad de horas extras añadidas"
                    + " a este empleado excedio el limite. No podra agregar mas horas extras en esta quincena.", "Error Al Agregar Hora Extra", JOptionPane.ERROR_MESSAGE);
        }

        if(!existeHoraExtra(horaExtra))
        {
            DialogCrearHoraExtra dCHE = new DialogCrearHoraExtra(SIRHApp.getInstance().getVentanaPrincipal(), 
                    true, indice, horaExtra, propioPanel, boton, empleado);
            dCHE.setVisible(true);
        }

        else 
        {
            horaExtra = buscarHoraExtra(boton);
            DialogModificarHoraExtra dMHE = new DialogModificarHoraExtra(SIRHApp.getInstance().getVentanaPrincipal(), true, indice,  horaExtra, propioPanel, boton);
            dMHE.setVisible(true);
        }
    }


    public int cantidadHorasExtras(List<HoraExtra> horasExtras){
        int cantidad = 0;
        for (HoraExtra he: horasExtras){
            cantidad += he.getCantidadHoras();
        }

        return cantidad;
    }

    private HoraExtra buscarHoraExtra(JButton boton){

        int size = nuevasHorasExtras.size();

            for(int i=0;i<size;i++){
                if( nuevasHorasExtras.get(i).getFecha().get(Calendar.DATE) == Integer.parseInt(boton.getText())){
                    return nuevasHorasExtras.get(i);
                }
            }

            return null;
    }

    private boolean existeHoraExtra(HoraExtra horaExtra){
        for(HoraExtra he: nuevasHorasExtras)
        {
            if(he.getFecha().get(Calendar.DATE) == horaExtra.getFecha().get(Calendar.DATE)){
                return true;
            }
        }

        return false;
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        botonAtras = new javax.swing.JButton();
        labelHorasDia = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        labelTitulo = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        labelNombreEmpleado = new javax.swing.JLabel();
        panelCalendario = new javax.swing.JPanel();

        botonAtras.setText("Atras");
        botonAtras.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonAtrasActionPerformed(evt);
            }
        });

        labelHorasDia.setFont(new java.awt.Font("Dialog", 0, 12));

        jLabel1.setText("Agregue horas extra haciendo clic en el dia que desea:");

        labelTitulo.setFont(new java.awt.Font("Dialog", 1, 24));
        labelTitulo.setText("Editando Horas Extras");

        jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/frontera/recursos/hExtra.png"))); // NOI18N

        labelNombreEmpleado.setFont(new java.awt.Font("Dialog", 1, 24));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelTitulo)
                    .addComponent(labelNombreEmpleado, javax.swing.GroupLayout.PREFERRED_SIZE, 394, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 394, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(labelTitulo, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(17, 17, 17)
                        .addComponent(labelNombreEmpleado, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel1)))
                .addGap(12, 12, 12))
        );

        panelCalendario.setLayout(new java.awt.GridLayout());

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(panelCalendario, javax.swing.GroupLayout.DEFAULT_SIZE, 415, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(109, 109, 109)
                                .addComponent(labelHorasDia, javax.swing.GroupLayout.DEFAULT_SIZE, 79, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(76, 76, 76)
                                .addComponent(botonAtras, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(42, 42, 42)
                        .addComponent(labelHorasDia, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 218, Short.MAX_VALUE)
                        .addComponent(botonAtras))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(panelCalendario, javax.swing.GroupLayout.DEFAULT_SIZE, 298, Short.MAX_VALUE)))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void botonAtrasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonAtrasActionPerformed
        SIRHApp.getInstance().getVentanaPrincipal().setPanelPersonalEmpleado(panel);    
}//GEN-LAST:event_botonAtrasActionPerformed

    public List<HoraExtra> getNuevasHorasExtras() {
        return nuevasHorasExtras;
    }

    public void setNuevasHorasExtras(ArrayList<HoraExtra> nuevasHorasExtras) {
        this.nuevasHorasExtras = nuevasHorasExtras;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonAtras;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel labelHorasDia;
    private javax.swing.JLabel labelNombreEmpleado;
    private javax.swing.JLabel labelTitulo;
    private javax.swing.JPanel panelCalendario;
    // End of variables declaration//GEN-END:variables
    private JPanel panel;
    private Empleado empleado;
    private List<HoraExtra> nuevasHorasExtras;
    private PanelHorasExtraEmpleado propioPanel;
}
